<?php

/**
 * 后台 案例模块管理　模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: case.php 32 2011-07-11 15:57:51Z zwt007 $
 * http://www.t-mac.org；
 */
class caseAction extends Action
{

    private $tmp_model;
    private $article_model;
    private $archives_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('casemodel');
        $this->article_model = Tmac::model('article');
        $this->archives_model = Tmac::model('archives');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin');
    }

    /**
     * 案例赏析类别管理 首页
     */
    public function index()
    {
        $cat_id = intval($this->getParam('cat_id'));
        $channelid = intval($this->getParam('channelid'));
        //TODO  取出所有案例赏析
        $rs = $this->article_model->getArticleList($channelid, '', $cat_id, $channelid);

        $this->assign('channelid', $channelid);
        $this->V('article');
    }

    /**
     * 新增/修改文章页面
     */
    public function add()
    {
        $aid = intval($this->getParam('aid'));
        $channeltype = 2;
        $cat_id = intval($this->getParam('cat_id'));
        if ($aid > 0) {
            $editinfo = $this->tmp_model->getArticleInfo($aid);
            $img = $this->tmp_model->getCfgBody('img', $editinfo['imgurls']);
            $imgurl_array = $img[2];
            $thumbtitle_array = $img[1];
        } else {
            $editinfo = array('title' => '', 'author' => '', 'order' => '', 'content' => '', 'cat_id' => $cat_id, 'article_id' => '', 'attachments' => '', 'keywords' => '', 'description' => '');
            $imgurl_array = array();
            $thumbtitle_array = array();
        }


        //取文章栏目
        $treers = $this->article_model->article_cat_tree(0, 0, $editinfo['cat_id'], $channeltype);

        //初始化一下    默认state_radio
        $editinfo['author'] = !empty($editinfo['author']) ? $editinfo['author'] : '默认管理员';
        $editinfo['order'] = !empty($editinfo['order']) ? $editinfo['order'] : '0';

        $this->assign('editinfo', $editinfo);
        $this->assign('imgurl_array', $imgurl_array);
        $this->assign('thumbtitle_array', $thumbtitle_array);
        $this->assign('treers', $treers);
        //TODO　载入资讯类别添加表单
        $this->V('case');
    }

    /**
     * 新增/修改栏目页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $aid = intval($this->getParam('aid'));
        $cat_id = intval($this->getParam('cat_id'));
        $title = trim($this->H($this->getParam('title')));
        $author = trim($this->H($this->getParam('author')));
        $order = intval($this->getParam('order'));
        $content = $this->getParam('content');
        $imgurl = $this->H($this->getParam('thumb'));
        $thumbdes = $this->H($this->getParam('thumbdes'));
        $content = $this->getParam('content');
        $keywords = $this->H($this->getParam('keywords'));
        $description = $this->H($this->getParam('description'));
        $type = 'images';

        $channelid = 2;

        if (!$this->archives_model->checkChannel($cat_id, $channelid)) {
            $this->redirect("你所选择的栏目与当前模型不相符！");
            exit();
        }

        //判断是否为空
        if (empty($title)) {
            $this->redirect('标题不能力空');
            exit;
        }

        if (empty($cat_id)) {
            $this->redirect('请选择分类');
            exit;
        }

        if (empty($imgurl)) {
            $this->redirect('请上传图片');
            exit;
        } else {            
            $imgurls = '';
            if (is_array($imgurl)) {
                foreach ($imgurl AS $k => $v) {
                    $thumbtitle = $thumbdes[$k];
                    $imgurls.="{img key=\"$thumbtitle\"}$v{/img}";
                }
            }
        }

        $info = array(
            'cat_id' => $cat_id,
            'channel' => $channelid,
            'title' => $title,
            'author' => $author,
            'order' => $order,
            'keywords' => $keywords,
            'description' => $description,
            'time' => time()
        );

        $type_url = PHP_SELF . '?m=archives.arclist&channelid=' . $channelid . '&cat_id=' . $cat_id;
        if ($aid > 0) {
            $article_page = HttpResponse::getCookie($cat_id . '_page');
            //update save article
            $where = 'article_id = ' . $aid . '';
            $rs = $this->tmp_model->addArticle('article', $info, 'UPDATE', $where);
            if ($rs) {
                $info_addonarticle = array('article_id' => $aid, 'cat_id' => $cat_id, 'imgurls' => $imgurls, 'content' => $content);
                $rs = $this->tmp_model->addArticle('addon', $info_addonarticle, 'UPDATE', $where);
                if ($rs) {
                    $this->redirect('修改案例赏析成功', $type_url . '&page= ' . $article_page . '');
                } else {
                    $this->redirect('修改案例赏析从表失败，请联系技术支持检查原因！');
                }
            } else {
                $this->redirect('修改案例赏析失败');
            }
        } else {
            //insert save article_class
            $article_id = $this->tmp_model->addArticleReturn($info);
            if ($article_id) {
                $info_addonarticle = array('article_id' => $article_id, 'cat_id' => $cat_id, 'imgurls' => $imgurls, 'content' => $content);
                $rs = $this->tmp_model->addArticle('addon', $info_addonarticle, 'INSERT');
                if ($rs) {
                    $this->redirect('添加案例赏析成功', $type_url);
                } else {
                    $this->redirect('添加案例赏析从表失败，请联系技术支持检查原因！');
                }
            } else {
                $this->redirect('添加案例赏析主表失败，请联系技术支持检查原因！');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $aid = intval($this->getParam('aid'));
        $id_a = $this->getParam('id_a');

        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($aid)) {
            $id = $aid;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delArticle($id);
        // TODO DEL该分类下的所有案例赏析
        if ($rs) {
            $this->redirect('删除案例赏析成功');
        } else {
            $this->redirect('删除案例赏析失败，请重试！');
        }
    }

}